/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Mike
 */
import com.raitcon.hibernate.bean.Departamento;
import com.raitcon.hibernate.bean.Pais;
import com.raitcon.hibernate.db.DepartamentoDB;
import com.raitcon.hibernate.db.PaisDB;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author javatutorials.co.in
 */
@ManagedBean(name = "inputBean")
@ViewScoped
public class InputBean implements Serializable {

    private static final long serialVersionUID = 9040359120893077422L;
    private static final Log log = LogFactory.getLog(PaisDB.class);
    private List<SelectItem> countries = new ArrayList<SelectItem>();
    private List<SelectItem> states = new ArrayList<SelectItem>();
    private String selectedCountry = "0";
    private String selectedState = "0";

    public InputBean() {
    }

    /**
     * Initialize the drop down list of countries
     */
    @PostConstruct
    public void initialize() {
        if (countries.size() > 0) {
            return;
        }

        PaisDB paisDb = new PaisDB();
        List<Pais> listPais = paisDb.getPaisAll();

        countries.add(new SelectItem(0, "--------"));

        for (Pais pais : listPais) {
            countries.add(new SelectItem(pais.getCodPais(), pais.getDescripcion()));
            /*countries.add(new SelectItem(2,"Country2"));
             countries.add(new SelectItem(3,"Country3"));*/
        }

        states.add(new SelectItem(0, "--------"));
        System.out.println("selectedState::::" + selectedState);

    }

    /**
     * Method is invoked by ajax request due to <f:ajax> tag parameter
     * listener="#{inputBean.setStateOptions()}" Set states options based on
     * country selected from country menu.
     */
    public void setStateOptions() {

        String codPais = this.selectedCountry;
        System.out.println("Selected codPais:" + codPais);
        states.clear();
        try {
            if (!this.selectedCountry.equals("0")) {
                DepartamentoDB departamentoDB = new DepartamentoDB();
                List<Departamento> lisDpto = departamentoDB.getDepartamentoByIdPais(codPais);
                for (Departamento dpto : lisDpto) {
                    states.add(new SelectItem(dpto.getCodDepartamento(), dpto.getDescripcion()));

                }
            } else {
                states.add(new SelectItem(0, "-------"));
            }

        } catch (Exception e) {
            log.error("Error:" + e.getMessage());

        }


    }

    // ------ Getters and Setters ------ 
    public List<SelectItem> getCountries() {
        return countries;
    }

    public List<SelectItem> getStates() {
        return states;
    }

    public String getSelectedCountry() {
        return selectedCountry;
    }

    public void setSelectedCountry(String selectedCountry) {
        this.selectedCountry = selectedCountry;
    }

    public String getSelectedState() {
        return selectedState;
    }

    public void setSelectedState(String selectedState) {
        this.selectedState = selectedState;
    }
}